<?php
declare (strict_types=1);

namespace app\service;

use app\model\TSubCategory;
use app\traits\ServiceTrait;
use app\util\TaobaoApiUtil;
use think\facade\Db;

/**
 * SubCategoryService
 * Class SubCategoryService
 * @package app\service
 */
class SubCategoryService
{
    //模型，带命名空间
    public static $model = 'app\model\TSubCategory';
    //模型主键
    public static $pk = 'id';
    //name字段名称
    public static $name = 'name';

    use ServiceTrait;

    /**
     * @param $tbGoodsId
     * @return array
     */
    public static function queryListByTbGoodsId($tbGoodsId)
    {
        $data = TaobaoApiUtil::getItemInfo($tbGoodsId);
        if (empty($data)) {
            return [];
        }
        $table = TSubCategory::$_table;
        $goods = [];
        $subCategoryList = [];
        foreach ($data as $item) {
            $cat_leaf_name = $item->cat_leaf_name;
            $subCat = Db::table($table)->where([['cat_leaf_name', '=', $cat_leaf_name]])->find();
            $category_id = $subCat['category_id'];
            $coupon = TaobaoApiUtil::getShareConvert($item->num_iid);
            $goods[] = [
                'catLeafName' => $item->cat_leaf_name,
                'catName' => $item->cat_name,
                'categoryId' => $category_id,
                'categorySupId' => '',
                'couponClickUrl' => $coupon['coupon_click_url'] ?? '',
                'couponEndTime' => '',
                'couponInfo' => '',
                'couponStartTime' => '',
                'createTime' => '',
                'daZhi' => '',
                'designerId' => '',
                'designerName' => '',
                'dimensions' => '',
                'goodsReport' => '',
                'id' => '',
                'keyword' => '',
                'labels' => '',
                'maxCommissionRate' => $coupon['max_commission_rate'] ?? '',
                'pictUrl' => $item->pict_url,
                'price' => $item->zk_final_price,
                'remark' => '',
                'shopName' => $item->nick,
                'shopType' => 1,
                'shopTypeName' => strstr($item->item_url, 'tmall') ? '天猫' : '淘宝',
                'status' => '',
                'tbGoodsId' => $item->num_iid,
                'tbkItemUrl' => $coupon['item_url'] ?? $item->item_url,
                'tbkTklUrl' => '',
                'title' => $item->title,
                'updateTime' => '',
                'zkFinalPrice' => $item->zk_final_price,
                'smallImages' => $item->small_images->string,
            ];
            $subCategoryList[] = [
                'catLeafName' => $subCat['cat_leaf_name'],
                'categoryId' => $category_id,
                'categoryName' => '',
                'createTime' => '',
                'id' => $subCat['id'],
                'name' => $subCat['name'],
                'remark' => '',
                'updateTime' => '',
            ];
        }

        return [
            'goods' => $goods,
            'subCategoryList' => $subCategoryList
        ];
    }
}
